var prefix = "/api/system/user";
$(function () {
    load();

    //自动刷新绑定
    $("#workNo,#userName").keydown(function (e) {
        if (e.keyCode == 13) {
            reload();
        }
    });
});

function load() {
    $('#exampleTable').bootstrapTable(
        {
            method: 'get',
            url: prefix + "/list",
            iconSize: 'outline',
            toolbar: '#exampleToolbar',
            striped: true,
            dataType: "json",
            pagination: true,
            clickToSelect: true,
            pageSize: 10,
            pageNumber: 1,
            showColumns: false,
            sidePagination: "client",
            contentType: "application/x-www-form-urlencoded",

            onClickRow: function (row, $element) {
                $element.addClass('info').siblings().removeClass('info').end();
            },
            columns: [
                {
                    field: 'idx',
                    radio: true
                },
                {
                    title: '序号',
                    formatter: function (value, row, index) {
                        return index + 1;
                    }
                },
                {
                    field: 'workNo',
                    title: '用户工号'
                },
                {
                    field: 'userName',
                    title: '真实名称'
                },
                {
                    field: 'deptName',
                    title: '部门'
                },
                {
                    field: 'post',
                    title: '职位'
                },
                {
                    field: 'createdTime',
                    title: '同步时间'
                },

                {
                    title: '操作',
                    align: 'center',
                    formatter: function (value, row, index) {
                        return [
                            '<a class="btn btn-sm btn-info" href="#" title="授权" onclick="addRole(' + row.id + ')">',
                            '<i class="glyphicon glyphicon-edit"></i>',
                            '</a>&nbsp;&nbsp;',
                            '<a class="btn btn-sm btn-warning" href="#" title="查看角色" onclick="viewRole(' + row.id + ')">',
                            '<i class="glyphicon glyphicon-off"></i>',
                            '</a>&nbsp;&nbsp;'
                        ].join('');
                    }
                }]
        })
}

//刷新
function reload() {
    $("#exampleTable").bootstrapTable('refresh');
}

//授权
function addRole(userId) {
    layer.open({
        type : 2,
        title : '角色授权',
        maxmin : true,
        shadeClose : false, // 点击遮罩关闭层
        area : [ '40%', '520px' ],
        content : prefix + '/addRolePage/' + userId
    });
}
//查看
function viewRole(userId) {
    layer.open({
        type: 2,
        title: '查看',
        maxmin: true,
        shadeClose: false, // 点击遮罩关闭层
        area: ['40%', '520px'],
        content: prefix + '/viewRole/' + userId
    });
}
